﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Collections;
using System.Runtime.Remoting.Messaging;
using System.IO;

namespace SanguoSharp
{

    internal class MethodRestrictionServerSink : IServerChannelSink
    {
        MethodRestrictionServerSinkProvider provider;
        IServerChannelSink next;

        public IServerChannelSink NextChannelSink
        {
            get { return next; }
        }

        public IDictionary Properties
        {
            get { return null; }
        }

        public MethodRestrictionServerSink(MethodRestrictionServerSinkProvider provider, IServerChannelSink next)
        {
            this.provider = provider;
            this.next = next;
        }

        public void AsyncProcessResponse(IServerResponseChannelSinkStack sinkStack, object state, IMessage msg, ITransportHeaders headers, Stream stream)
        {
            sinkStack.AsyncProcessResponse(msg, headers, stream);
        }
        public ServerProcessing ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, out IMessage responseMsg, out ITransportHeaders responseHeaders, out Stream responseStream)
        {
            if (requestMsg is IMethodCallMessage)
            {
                IMethodCallMessage call = (IMethodCallMessage)requestMsg;
                if (!provider.Filter(call.MethodBase))
                    throw new MemberAccessException("You are not allowed to access this member via remoting!");
            }
            return next.ProcessMessage(sinkStack, requestMsg, requestHeaders, requestStream, out responseMsg, out responseHeaders, out responseStream);
        }

        public Stream GetResponseStream(IServerResponseChannelSinkStack sinkStack, object state, IMessage msg, ITransportHeaders headers)
        {
            return null;
        }
    }
}
